package com.rmb.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rmb.common.model.joinstatus.JoinStatus;
import org.apache.ibatis.jdbc.SQL;

import java.util.*;

/**
 * @author 段段
 * @PackageName: com.rmb.util
 * @ClassName: JsonToObject
 * @Description: json转SQL
 * @data: 2021/1/11 20:03
 **/
public class JsonToObject {

    /**
     * json转SQL语句
     * @param josn json字符串
     * @return
     */
    public static List<Map<String, Object>>  getJson(String josn) {

        HashMap<String, Object> mapString = new HashMap<>(16);

        List<Map<String,Object>> objectList = new ArrayList<>();

        JSONObject jsonObject = JSONArray.parseObject(josn);
        System.out.println(jsonObject);
        Set<String> strings = jsonObject.keySet();
        for (String string : strings) {
            System.out.println(string);
            JSONArray jsonArray = jsonObject.getJSONArray(string);
            for (Object object : jsonArray) {

                Map<String, Object> map = (Map<String, Object>) (object);

                Set<String> strings1 = map.keySet();

                List<String> strings2 = new ArrayList<>();

                for (String s : strings1) {
                    strings2.add(s);
                }
                mapString.put(string,strings2);
            }
        }
        objectList.add(mapString);
        return objectList;
    }

    /**
     * 转SQL语句
     * @param json
     * @return
     */
    public static String select(List<Map<String,Object>> json) {
        //参数拼接
        StringBuilder cloum = new StringBuilder();
        for (Map<String, Object> map : json) {
            System.out.println(map);
            Set<String> stringSet = map.keySet();
            for (String s : stringSet) {
                Collection<Object> values = map.values();
                for (Object value : values) {
                    List<String> stringList = (List<String>) value;
                    for (String s1 : stringList) {
                        cloum.append(s+"."+s1+" ");
                    }
                }
            }
        }
        String clou = cloum.toString();
        //去重
        StringBuilder stringBuilder = new StringBuilder();
        String[] strings = clou.split(" ");
        Set<String> hashSet = new HashSet<>();
        for (String s1 : strings) {
            hashSet.add(s1);
        }
        for (String s1 : hashSet) {
            stringBuilder.append(s1+",");
        }
        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);

        //表名拼接
        StringBuilder cloum1 = new StringBuilder();
        for (Map<String, Object> map : json) {
            System.out.println(map);
            Set<String> stringSet = map.keySet();
            for (String s : stringSet) {
                cloum1.append(s+",");
            }
        }
        String s = cloum1.toString();
        String table = s.substring(0, s.length() - 1);
        System.out.println(table);

        return new SQL() {
            {
                if (table!=null&&clou!=null){
                    SELECT(substring);
                    FROM(table);
                }
            }
        }.toString();
    }

    public static String insert(List<Map<String,Object>> json) {

        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();

        for (Map<String, Object> map : json) {
            Set<String> stringSet = map.keySet();
            for (String s : stringSet) {
                stringBuilder1.append(s);
            }
            Collection<Object> values = map.values();
            for (Object value : values) {
                List<String> va = (List<String>)value;
                for (String s : va) {
                    stringBuilder2.append(s+",");
                    stringBuilder.append("#"+"("+s+")"+",");
                }
            }
        }

        String s = stringBuilder.substring(0,stringBuilder.length()-1);
        String s1 = stringBuilder1.toString();
        String substring = stringBuilder2.substring(0, stringBuilder2.length() - 1);
        return new SQL() {
            {
                INSERT_INTO(s1);
                VALUES(substring,s);
            }
        }.toString();
    }

    public static String select(List<Map<String,Object>> json,String stringJoin) {
        JSONObject jsonObject = JSON.parseObject(stringJoin);
        String join = jsonObject.get("join").toString();
        String id = jsonObject.get("id").toString();
        String id1 = jsonObject.get("id1").toString();
        String groud = jsonObject.get("groud").toString();
        String orederby = jsonObject.get("orederby").toString();
        String orederName = jsonObject.get("orederName").toString();
        //参数拼接
        StringBuilder cloum = new StringBuilder();
        for (Map<String, Object> map : json) {
            Set<String> stringSet = map.keySet();
            for (String s : stringSet) {
                Collection<Object> values = map.values();
                for (Object value : values) {
                    List<String> stringList = (List<String>) value;
                    for (String s1 : stringList) {
                        cloum.append(s + "." + s1 + " ");
                    }
                }
            }
        }
        String clou = cloum.toString();
        //去重
        StringBuilder stringBuilder = new StringBuilder();
        String[] strings = clou.split(" ");
        Set<String> hashSet = new HashSet<>();
        for (String s1 : strings) {
            hashSet.add(s1);
        }
        for (String s1 : hashSet) {
            stringBuilder.append(s1 + ",");
        }
        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);
        List<String> strings1 = new ArrayList<>();
        //表名拼接
        StringBuilder cloum1 = new StringBuilder();
        for (Map<String, Object> map : json) {
            Set<String> stringSet = map.keySet();
            for (String s : stringSet) {
                strings1.add(s);
                cloum1.append(s + ",");
            }
        }
        String s = cloum1.toString();
        String table = s.substring(0, s.length() - 1);
        return new SQL() {
            {
                if (table != null && clou != null) {
                    SELECT(substring);
                    if (join.equals(JoinStatus.LEFT_JOIN)) {
                        FROM(strings1.get(0));
                        LEFT_OUTER_JOIN(strings1.get(1) + " on " + strings1.get(0) + "." + id + " = " + strings1.get(1) + "." + id1);
                    } else if (join.equals(JoinStatus.RIGHT_JOIN)) {
                        FROM(strings1.get(0));
                        RIGHT_OUTER_JOIN(strings1.get(1) + " on " + strings1.get(0) + "." + id + " = " + strings1.get(1) + "." + id1);
                    } else if (join.equals(JoinStatus.INNER_JOIN)) {
                        FROM(strings1.get(0));
                        INNER_JOIN(strings1.get(1) + " on " + strings1.get(0) + "." + id + " = " + strings1.get(1) + "." + id1);
                    } else {
                        FROM(table);
                    }
                    if (groud != null) {
                        GROUP_BY(groud);
                    }
                    if (orederby != null) {
                        if (orederby.equals(JoinStatus.DESCENDING_ORDER)) {
                            if (orederName != null) {
                                StringBuilder desc = new StringBuilder(String.valueOf(orederName)).append(" ").append("desc");
                                ORDER_BY(desc.toString());
                            }
                        }
                        if (orederby.equals(JoinStatus.ASCENDING_ORDER)) {
                            if (orederName != null) {
                                ORDER_BY(orederName);
                            }
                        }
                    }
                }
            }
        }.toString();
    }
}
